package com.atguigu.crowd.mvc.config;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import com.atguigu.crowd.common.CrowdConstant;
import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.exception.LoginFailedException;
import com.atguigu.crowd.util.CrowdUtil;
import com.atguigu.crowd.util.ResultEntity;
import com.google.gson.Gson;

/**
 * @ControllerAdvice 表示当前这个类时一个基于注解的异常处理器类
 * @ExceptionHandler 将一个具体的异常类型和一个方法联系起来
 * @author Administrator
 *
 */
@ControllerAdvice
public class CrowdExceptionResolver {
	
	@ExceptionHandler(value = LoginAcctAlreadyInUseForUpdateException.class)
	public ModelAndView LoginAcctAlreadyInUseForUpdateException(
			// 实际捕获到的异常类型
			LoginAcctAlreadyInUseForUpdateException exception,
			// 判断当前请求的类型
			HttpServletRequest request,
			// 当前响应对象
			HttpServletResponse response) throws IOException {
		
		String viewName = "admin-edit";

		return commonResolve(viewName, exception, request, response);

	}
	
	@ExceptionHandler(value = LoginAcctAlreadyInUseException.class)
	public ModelAndView resolveLoginAcctAlreadyInUseException(
			// 实际捕获到的异常类型
			LoginAcctAlreadyInUseException exception,
			// 判断当前请求的类型
			HttpServletRequest request,
			// 当前响应对象
			HttpServletResponse response) throws IOException {

		String viewName = "admin-add";

		return commonResolve(viewName, exception, request, response);

	}
	
	@ExceptionHandler(value = LoginFailedException.class)
	public ModelAndView resolveLoginFailedException(
			// 实际捕获到的异常类型
			LoginFailedException exception,
			// 判断当前请求的类型
			HttpServletRequest request,
			// 当前响应对象
			HttpServletResponse response) throws IOException {

		String viewName = "admin-login";

		return commonResolve(viewName, exception, request, response);

	}

	@ExceptionHandler(value = NullPointerException.class)
	public ModelAndView resolveNullPointerException(
			// 实际捕获到的异常类型
			NullPointerException exception,
			// 判断当前请求的类型
			HttpServletRequest request,
			// 当前响应对象
			HttpServletResponse response) throws IOException {

		String viewName = "system-error";

		return commonResolve(viewName, exception, request, response);

	}

	private ModelAndView commonResolve(String viewName, Exception exception, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// 1.先判断一下请求的类型
		boolean judgeResult = CrowdUtil.judgeRequestType(request);

		// 2.如果是ajax请求
		if (judgeResult) {

			// 3. 创建ResultEntity对象
			ResultEntity<Object> resultEntity = ResultEntity.failed(exception.getMessage());

			// 4.创建Gson对象
			Gson gson = new Gson();

			// 5.将ResultEntity对象转换为JSON字符串
			String json = gson.toJson(resultEntity);

			// 6.将JSON字符串作为响应体返回给浏览器
			response.getWriter().write(json);

			// 7.由于上面已经通过原生的response对象返回了响应，所以不提供ModelAndView对象

			return null;
		}

		// 8.如不过是ajax请求，则创建ModelAndView对象
		ModelAndView modelAndView = new ModelAndView();

		// 9.将Exception对象存入模型
		modelAndView.addObject(CrowdConstant.ATTR_NAME_EXCEPTION, exception);

		// 10.设置对应的视图
		modelAndView.setViewName(viewName);

		// 11.返回ModelAndView对象
		return modelAndView;
	}

}
